package isp.apps.example.shoppingcart.persistence.hibernate;

import isp.apps.example.shoppingcart.domain.CartItem;

import isp.apps.example.shoppingcart.persistence.CartItemsDirectory;

import isp.apps.example.shoppingcart.persistence.PersistenceManagerFactory;

import isp.utils.cjis.persistence.PersistenceException;
import isp.utils.cjis.persistence.PersistenceManager;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HibernateCartItemsDirectory implements CartItemsDirectory
{
  private PersistenceManager persistenceManager = PersistenceManagerFactory
    .getPersistenceManager();

  private static final Log log = LogFactory.getLog(HibernateCartItemsDirectory.class);
    
  public HibernateCartItemsDirectory()
  {
  }

  public CartItem getCartItemById(Integer cartItemId)
  {
    try
    {
      return (CartItem) persistenceManager.load(CartItem.class, cartItemId);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }
  
  public List getCartItemsByItemId(Integer itemId)
  {
    try
    {
      List parameters = new LinkedList();
      parameters.add(itemId);
      return persistenceManager.find("get.cartItemsByItemId", parameters);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }
  
  public List getCartItemsByOrder(Integer orderId)
  {
    try
    {
      List parameters = new LinkedList();
      parameters.add(orderId);
      return persistenceManager.find("get.cartItems", parameters);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }
}